package com.example.myapplication.screen

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun RadioButtonDemo(innerPadding: PaddingValues){
    var gender by  remember { mutableStateOf("男") }

    //观察状态变量的变化
    LaunchedEffect(gender) {
        println("当前选择的性别时："+gender)
    }

    Column(
        modifier = Modifier.fillMaxWidth().padding(innerPadding),
        verticalArrangement = Arrangement.Top,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Row(
            modifier = Modifier.height(50.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            RadioButton(
                selected = gender=="男",
                onClick = {
                    gender = "男"
                }
            )
            Text(text = "男")
        }

        Row(
            modifier = Modifier.height(50.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            RadioButton(
                selected = gender=="女",
                onClick = {
                    gender = "女"
                }
            )
            Text(text = "女")
        }
    }
}